package com.tool;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ToolSpring {

    private static final Map<String, ? super Object> instanceMap = new HashMap<>();

    /**
     * 通过 spring 获取ioc对象
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T extends Object> T get(Class<T> clazz) {
        if (instanceMap.containsKey(clazz.getName())) {
            return (T) instanceMap.get(clazz.getName());
        }
        T bean = SpringUtil.getBean(clazz);
        instanceMap.put(clazz.getName(), bean);
        return bean;
    }

    /**
     * 获取客户端IP地址
     */
    public static String ip() {
        String ip = ServletUtil.getClientIP(Objects.requireNonNull(getRequest()));
        if (ip != null && ip.length() > 15) { // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        return ip;
    }

    /**
     * 获取客户端主机名称
     */
    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
        }
        return "未知";
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getResponse();
    }

    public static HttpSession getSession() {
        return Objects.requireNonNull(ToolSpring.getRequest()).getSession();
    }

    public static String getPara(String name) {
        return Objects.requireNonNull(ToolSpring.getRequest()).getParameter(name);
    }

    public static String[] getParaArray(String name) {
        return Objects.requireNonNull(ToolSpring.getRequest()).getParameterValues(name);
    }

    public static Map<String, String> getRequestParameters() {
        HashMap<String, String> values = new HashMap();
        HttpServletRequest request = getRequest();
        if (request == null) {
            return values;
        } else {
            Enumeration enums = request.getParameterNames();

            while(enums.hasMoreElements()) {
                String paramName = (String)enums.nextElement();
                String paramValue = request.getParameter(paramName);
                values.put(paramName, paramValue);
            }
            return values;
        }
    }
}
